package com.bjpowernode.controller;

import com.alibaba.excel.EasyExcel;
import com.bjpowernode.config.listener.UploadDataListener;
import com.bjpowernode.model.TClue;
import com.bjpowernode.model.TUser;
import com.bjpowernode.query.ActivityQuery;
import com.bjpowernode.query.ClueQuery;
import com.bjpowernode.result.R;
import com.bjpowernode.service.ClueService;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@RestController
public class ClueController {

    @Resource
    private ClueService clueService;

    @GetMapping(value = "/api/clue")
    public R cluePage(@RequestParam(value = "current", required = false) Integer current) {
        //required = false表示current参数可以不传；
        //required = true表示current参数必须要传，不传会报错；
        if (current == null) {
            current = 1;
        }

        PageInfo<TClue> pageInfo = clueService.getClueByPage(current);

        return R.OK(pageInfo);
    }

    /**
     * 导入线索的Excel
     *
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping(value = "/api/importExcel")
    public R importExcel(MultipartFile file, @RequestHeader(value = "Authorization") String jwt) throws IOException {

        clueService.importExcel(file.getInputStream(), jwt);

        return R.OK();
    }

    @GetMapping(value = "/api/clue/{phone}")
    public R checkPhone(@PathVariable(value = "phone") String phone) {
        Boolean check = clueService.checkPhone(phone);
        //check是true表示手机号存在，false表示不存在
        return check ? R.FAIL() : R.OK(); //返回状态码500表示手机号存在，返回200表示手机号不存在
    }

    /**
     * 录入线索（提交保存）
     *
     * @param clueQuery
     * @param jwt
     * @return
     */
    @PostMapping(value = "/api/clue")
    public R addClue(ClueQuery clueQuery, @RequestHeader(value = "Authorization") String jwt) {
        clueQuery.setJwt(jwt);
        int save = clueService.saveClue(clueQuery);
        return save >= 1 ? R.OK() : R.FAIL();
    }

    /**
     * 根据id查询线索详情
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/api/clue/detail/{id}")
    public R clueDetail(@PathVariable(value = "id") Integer id) {
        TClue tClue = clueService.getClueById(id);
        return R.OK(tClue);
    }

    /**
     * 编辑线索（提交保存）
     *
     * @param clueQuery
     * @param jwt
     * @return
     */
    @PutMapping(value = "/api/clue")
    public R editClue(ClueQuery clueQuery, @RequestHeader(value = "Authorization") String jwt) {
        clueQuery.setJwt(jwt);
        int update = clueService.updateClue(clueQuery);
        return update >= 1 ? R.OK() : R.FAIL();
    }

    /**
     * 根据id删除线索
     *
     * @param id
     * @return
     */
    @PreAuthorize(value = "hasAuthority('clue:delete')")
    @DeleteMapping(value = "/api/clue/{id}")
    public R delClue(@PathVariable(value = "id") Integer id) {
        int del = clueService.delClueById(id);
        return del >= 1 ? R.OK() : R.FAIL();
    }

    /**
     * 根据id批量删除线索
     *
     * @param ids
     * @return
     */
    @PreAuthorize(value = "hasAuthority('clue:delete')")
    @DeleteMapping(value = "/api/clue")
    public R batchDelClue(@RequestParam(value = "ids") String ids) {
        // ids = "1,3,5,6,11,13,16";
        List<String> idList = Arrays.asList(ids.split(","));

        int batchDel = clueService.batchDelClueById(idList);
        return batchDel >= idList.size() ? R.OK() : R.FAIL();
    }
}
